package com.cc.gulimall.seckill.interceptor;


import com.cc.common.constant.AuthServerConstant;
import com.cc.common.vo.MemberRsepVo;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * <p>Title: LoginUserInterceptor</p>
 * Description：
 * date：2020/7/9 15:58
 */
/*前端拦截不靠谱，还是得靠后端拦截*/
@Component
public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberRsepVo> threadLocal = new ThreadLocal<>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String uri = request.getRequestURI();
        // 拦截秒杀服务
        boolean match = new AntPathMatcher().match("/kill", uri);
        if (match) {
            HttpSession session = request.getSession();
            MemberRsepVo memberRsepVo = (MemberRsepVo) session.getAttribute(AuthServerConstant.LOGIN_USER);
            if (memberRsepVo != null) {
                threadLocal.set(memberRsepVo);
                return true;
            } else {
                // 没登陆就去登录
                session.setAttribute("msg", AuthServerConstant.NOT_LOGIN);
                response.sendRedirect("http://auth.gulimall.com/login.html");
                return false;
            }
        }
        /*其余请求直接放行*/
        return true;
    }
}
